import { createRouter, createWebHashHistory } from 'vue-router'
import { getToken } from '@/utils/auth'

const routes = [
    {
        path: '/',
        name: 'home',
        component: () => import('@/views/dashboard/index.vue')
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login/index.vue')
    },
    {
        path: '/details',
        name: 'details',
        component: () => import('@/views/details/index.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next();
    } else {
        let token = getToken();
        if (token === null || token === '' || token === undefined) {
            next('/login');
        } else {
            next();
        }
    }
});

export default router

